package com.li.springbootproject.service.test.impl;

import com.alibaba.fastjson.JSON;
import com.li.springbootproject.domain.User;
import com.li.springbootproject.domain.test.Progress;
import com.li.springbootproject.mapper.UserMapper;
import com.li.springbootproject.service.test.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 6/19/2022 10:26 AM
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Progress getProgress(String key) {
        key = "1fa9fd18-c341-43f7-9f02-691c52adf51d";
        Object object = redisTemplate.opsForValue().get(key);
        if (!Objects.isNull(object)) {
            Progress progress = JSON.parseObject(String.valueOf(object), Progress.class);
            return progress;
        }
        return null;
    }

    @Override
    public void insertDemo() {
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            User user = new User();
            user.setId(i);
            list.add(user);
            user.setUsername("测试" + i);
        }
        Progress progress = new Progress();
        String uuid = "1fa9fd18-c341-43f7-9f02-691c52adf51d";
        int flag = 0;
        for (User user : list) {
            userMapper.insertUser(user);
            progress.setUuid(uuid);
            progress.setMessage("第" + flag + 1 + " 条数据导入成功！");
            double num = (flag + 1) / 10000.0 * 100;
            progress.setProgressNum(num);
            progress.createProgress(progress);
            flag++;
        }
    }
}
